<?php
class Activities extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function getAllActivity($id = '',$user='',$sort='',$order='',$offset='',$paging='')
    {
        $data = array();
        $sql  = "SELECT *,at.id ";
        $sql .=" FROM activity_transaction at ";
		$sql .=" LEFT JOIN customers c ON c.id=at.customer_id ";
		$sql .= " WHERE at.pic<>'' ";
		if($id){
			$sql.=	" AND at.customer_id =".$id."";
		}
		if($user){
			$sql.=	" AND at.pic ='".$user."'";
		}
		
		if($sort){
			$sql.=	" ORDER BY at.".$sort." ".$order;
		}
		
		if($paging){
			$sql.=	" LIMIT ";
			 if($offset){
				$sql.=	$offset.",";
			}else{
				$sql.=	"0,";
			}
			$sql.=	$paging;
		}
        $Q = $this->db->query($sql);
        if ($Q->num_rows() > 0):
            foreach ($Q->result_array() as $row):
                $data[] = $row;
            endforeach;
        endif;
		$Q->free_result();
        return $data;
	}
	
	function getCountActivity($id='',$pic=''){
        $data=array();
		$sql =	" SELECT * ";
        $sql.=	" FROM activity_transaction at WHERE at.id >=0";
        if($id){
            $sql.=	" AND at.id='".$id."'";
        }
		if($pic){
            $sql.=	" AND at.pic='".$pic."'";
        }
        $Q	 =	$this->db->query($sql);
		return $Q->num_rows();
    }
	
	function getSave(){  
        $time = date('H:i:s');
		$date = date('Y-m-d'); 
		$this->db->where('customer_name',rtrim($this->input->post('customer')));
		$Q=$this->db->get('customers');
		$val=$Q->row_array();
		if(count($val)>0)
			$customer=$val['id'];
		else
			$customer=0;
			
        $value = array  (
                            'activity_desc'     =>  $this->input->post('desc'),
                            'activity_id'       =>  $this->input->post('type'),
                            'customer_id'       =>  $customer,
                            'pic'               =>  $this->session->userdata('user_id'),
                            'activity_time'     =>  $time,
                            'activity_date'     =>  $date,
                            'activity_location' =>  $this->input->post('location'),
                            'activity_people'   =>  $this->input->post('participant'),
                            'activity_objective'=>  $this->input->post('objective'),
                            'activity_result'   =>  $this->input->post('result'),
                            'activity_follow_up'=>  $this->input->post('follow_up'),
                            'user_stamp'		=>  $this->session->userdata('user_id'),
                            'date_stamp'		=>  date('Y-m-d')
                        );
        $this->db->insert('activity_transaction',$value); 
    }
	
	function remove($id)
    {
        $id = trim($id);
        $id = strtolower($id);
        $this->db->where('id', $id);
        $this->db->delete('activity_transaction');
    }
	
	
	
	
}	